********************************************************************************
** 	TITLE:		de2009_gles_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ZA5302_en_v6-0-2                                              **
**	DATE:		October 2022 					                              **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "de2009_gles_contact_networks", replace

* Open the dataset

use "ZA5302_en_v6-0-2.dta", clear


*****************************
** DISTRICT-LEVEL OUTCOMES **
*****************************

* Note: 1 = CDU/CSU, 4 = SPD, 5 = FDP, 6 = Green, 7 = Die Linke

gen district_outcome = .

replace district_outcome = 1 if wkrnr == 1
replace district_outcome = 1 if wkrnr == 2
replace district_outcome = 1 if wkrnr == 3
replace district_outcome = 1 if wkrnr == 4
replace district_outcome = 4 if wkrnr == 5
replace district_outcome = 1 if wkrnr == 6
replace district_outcome = 1 if wkrnr == 7
replace district_outcome = 1 if wkrnr == 8
replace district_outcome = 1 if wkrnr == 9
replace district_outcome = 1 if wkrnr == 10
replace district_outcome = 4 if wkrnr == 11
replace district_outcome = 1 if wkrnr == 12
replace district_outcome = 1 if wkrnr == 13
replace district_outcome = 7 if wkrnr == 14
replace district_outcome = 1 if wkrnr == 15
replace district_outcome = 1 if wkrnr == 16
replace district_outcome = 1 if wkrnr == 17
replace district_outcome = 1 if wkrnr == 18
replace district_outcome = 4 if wkrnr == 19
replace district_outcome = 4 if wkrnr == 20
replace district_outcome = 1 if wkrnr == 21
replace district_outcome = 1 if wkrnr == 22
replace district_outcome = 1 if wkrnr == 23
replace district_outcome = 4 if wkrnr == 24
replace district_outcome = 4 if wkrnr == 25
replace district_outcome = 1 if wkrnr == 26
replace district_outcome = 4 if wkrnr == 27
replace district_outcome = 1 if wkrnr == 28
replace district_outcome = 1 if wkrnr == 29
replace district_outcome = 1 if wkrnr == 30
replace district_outcome = 1 if wkrnr == 31
replace district_outcome = 1 if wkrnr == 32
replace district_outcome = 1 if wkrnr == 33
replace district_outcome = 1 if wkrnr == 34
replace district_outcome = 1 if wkrnr == 35
replace district_outcome = 1 if wkrnr == 36
replace district_outcome = 1 if wkrnr == 37
replace district_outcome = 1 if wkrnr == 38
replace district_outcome = 1 if wkrnr == 39
replace district_outcome = 1 if wkrnr == 40
replace district_outcome = 4 if wkrnr == 41
replace district_outcome = 4 if wkrnr == 42
replace district_outcome = 4 if wkrnr == 43
replace district_outcome = 4 if wkrnr == 44
replace district_outcome = 1 if wkrnr == 45
replace district_outcome = 4 if wkrnr == 46
replace district_outcome = 4 if wkrnr == 47
replace district_outcome = 4 if wkrnr == 48
replace district_outcome = 4 if wkrnr == 49
replace district_outcome = 4 if wkrnr == 50
replace district_outcome = 4 if wkrnr == 51
replace district_outcome = 1 if wkrnr == 52
replace district_outcome = 4 if wkrnr == 53
replace district_outcome = 4 if wkrnr == 54
replace district_outcome = 4 if wkrnr == 55
replace district_outcome = 4 if wkrnr == 56
replace district_outcome = 4 if wkrnr == 57
replace district_outcome = 7 if wkrnr == 58
replace district_outcome = 4 if wkrnr == 59
replace district_outcome = 7 if wkrnr == 60
replace district_outcome = 4 if wkrnr == 61
replace district_outcome = 4 if wkrnr == 62
replace district_outcome = 4 if wkrnr == 63
replace district_outcome = 7 if wkrnr == 64
replace district_outcome = 7 if wkrnr == 65
replace district_outcome = 1 if wkrnr == 66
replace district_outcome = 7 if wkrnr == 67
replace district_outcome = 1 if wkrnr == 68
replace district_outcome = 1 if wkrnr == 69
replace district_outcome = 7 if wkrnr == 70
replace district_outcome = 1 if wkrnr == 71
replace district_outcome = 7 if wkrnr == 72
replace district_outcome = 7 if wkrnr == 73
replace district_outcome = 1 if wkrnr == 74
replace district_outcome = 7 if wkrnr == 75
replace district_outcome = 4 if wkrnr == 76
replace district_outcome = 7 if wkrnr == 77
replace district_outcome = 1 if wkrnr == 78
replace district_outcome = 1 if wkrnr == 79
replace district_outcome = 1 if wkrnr == 80
replace district_outcome = 4 if wkrnr == 81
replace district_outcome = 1 if wkrnr == 82
replace district_outcome = 1 if wkrnr == 83
replace district_outcome = 6 if wkrnr == 84
replace district_outcome = 7 if wkrnr == 85
replace district_outcome = 7 if wkrnr == 86
replace district_outcome = 7 if wkrnr == 87
replace district_outcome = 1 if wkrnr == 88
replace district_outcome = 1 if wkrnr == 89
replace district_outcome = 1 if wkrnr == 90
replace district_outcome = 1 if wkrnr == 91
replace district_outcome = 1 if wkrnr == 92
replace district_outcome = 1 if wkrnr == 93
replace district_outcome = 4 if wkrnr == 94
replace district_outcome = 1 if wkrnr == 95
replace district_outcome = 4 if wkrnr == 96
replace district_outcome = 4 if wkrnr == 97
replace district_outcome = 1 if wkrnr == 98
replace district_outcome = 1 if wkrnr == 99
replace district_outcome = 1 if wkrnr == 100
replace district_outcome = 1 if wkrnr == 101
replace district_outcome = 4 if wkrnr == 102
replace district_outcome = 4 if wkrnr == 103
replace district_outcome = 1 if wkrnr == 104
replace district_outcome = 1 if wkrnr == 105
replace district_outcome = 1 if wkrnr == 106
replace district_outcome = 1 if wkrnr == 107
replace district_outcome = 1 if wkrnr == 108
replace district_outcome = 1 if wkrnr == 109
replace district_outcome = 1 if wkrnr == 110
replace district_outcome = 1 if wkrnr == 111
replace district_outcome = 1 if wkrnr == 112
replace district_outcome = 1 if wkrnr == 113
replace district_outcome = 1 if wkrnr == 114
replace district_outcome = 4 if wkrnr == 115
replace district_outcome = 4 if wkrnr == 116
replace district_outcome = 4 if wkrnr == 117
replace district_outcome = 4 if wkrnr == 118
replace district_outcome = 4 if wkrnr == 119
replace district_outcome = 4 if wkrnr == 120
replace district_outcome = 4 if wkrnr == 121
replace district_outcome = 4 if wkrnr == 122
replace district_outcome = 4 if wkrnr == 123
replace district_outcome = 4 if wkrnr == 124
replace district_outcome = 1 if wkrnr == 125
replace district_outcome = 4 if wkrnr == 126
replace district_outcome = 1 if wkrnr == 127
replace district_outcome = 1 if wkrnr == 128
replace district_outcome = 1 if wkrnr == 129
replace district_outcome = 1 if wkrnr == 130
replace district_outcome = 1 if wkrnr == 131
replace district_outcome = 1 if wkrnr == 132
replace district_outcome = 1 if wkrnr == 133
replace district_outcome = 4 if wkrnr == 134
replace district_outcome = 1 if wkrnr == 135
replace district_outcome = 4 if wkrnr == 136
replace district_outcome = 1 if wkrnr == 137
replace district_outcome = 1 if wkrnr == 138
replace district_outcome = 4 if wkrnr == 139
replace district_outcome = 4 if wkrnr == 140
replace district_outcome = 4 if wkrnr == 141
replace district_outcome = 4 if wkrnr == 142
replace district_outcome = 4 if wkrnr == 143
replace district_outcome = 4 if wkrnr == 144
replace district_outcome = 4 if wkrnr == 145
replace district_outcome = 4 if wkrnr == 146
replace district_outcome = 1 if wkrnr == 147
replace district_outcome = 1 if wkrnr == 148
replace district_outcome = 1 if wkrnr == 149
replace district_outcome = 1 if wkrnr == 150
replace district_outcome = 4 if wkrnr == 151
replace district_outcome = 1 if wkrnr == 152
replace district_outcome = 1 if wkrnr == 153
replace district_outcome = 1 if wkrnr == 154
replace district_outcome = 1 if wkrnr == 155
replace district_outcome = 1 if wkrnr == 156
replace district_outcome = 1 if wkrnr == 157
replace district_outcome = 1 if wkrnr == 158
replace district_outcome = 1 if wkrnr == 159
replace district_outcome = 1 if wkrnr == 160
replace district_outcome = 1 if wkrnr == 161
replace district_outcome = 1 if wkrnr == 162
replace district_outcome = 1 if wkrnr == 163
replace district_outcome = 1 if wkrnr == 164
replace district_outcome = 1 if wkrnr == 165
replace district_outcome = 1 if wkrnr == 166
replace district_outcome = 1 if wkrnr == 167
replace district_outcome = 4 if wkrnr == 168
replace district_outcome = 4 if wkrnr == 169
replace district_outcome = 4 if wkrnr == 170
replace district_outcome = 4 if wkrnr == 171
replace district_outcome = 4 if wkrnr == 172
replace district_outcome = 1 if wkrnr == 173
replace district_outcome = 1 if wkrnr == 174
replace district_outcome = 1 if wkrnr == 175
replace district_outcome = 1 if wkrnr == 176
replace district_outcome = 1 if wkrnr == 177
replace district_outcome = 1 if wkrnr == 178
replace district_outcome = 1 if wkrnr == 179
replace district_outcome = 1 if wkrnr == 180
replace district_outcome = 1 if wkrnr == 181
replace district_outcome = 1 if wkrnr == 182
replace district_outcome = 1 if wkrnr == 183
replace district_outcome = 1 if wkrnr == 184
replace district_outcome = 1 if wkrnr == 185
replace district_outcome = 4 if wkrnr == 186
replace district_outcome = 1 if wkrnr == 187
replace district_outcome = 1 if wkrnr == 188
replace district_outcome = 1 if wkrnr == 189
replace district_outcome = 1 if wkrnr == 190
replace district_outcome = 1 if wkrnr == 191
replace district_outcome = 1 if wkrnr == 192
replace district_outcome = 1 if wkrnr == 193
replace district_outcome = 7 if wkrnr == 194
replace district_outcome = 1 if wkrnr == 195
replace district_outcome = 1 if wkrnr == 196
replace district_outcome = 7 if wkrnr == 197
replace district_outcome = 1 if wkrnr == 198
replace district_outcome = 1 if wkrnr == 199
replace district_outcome = 1 if wkrnr == 200
replace district_outcome = 1 if wkrnr == 201
replace district_outcome = 1 if wkrnr == 202
replace district_outcome = 1 if wkrnr == 203
replace district_outcome = 1 if wkrnr == 204
replace district_outcome = 1 if wkrnr == 205
replace district_outcome = 1 if wkrnr == 206
replace district_outcome = 4 if wkrnr == 207
replace district_outcome = 1 if wkrnr == 208
replace district_outcome = 1 if wkrnr == 209
replace district_outcome = 4 if wkrnr == 210
replace district_outcome = 1 if wkrnr == 211
replace district_outcome = 1 if wkrnr == 212
replace district_outcome = 1 if wkrnr == 213
replace district_outcome = 1 if wkrnr == 214
replace district_outcome = 1 if wkrnr == 215
replace district_outcome = 1 if wkrnr == 216
replace district_outcome = 1 if wkrnr == 217
replace district_outcome = 1 if wkrnr == 218
replace district_outcome = 1 if wkrnr == 219
replace district_outcome = 1 if wkrnr == 220
replace district_outcome = 1 if wkrnr == 221
replace district_outcome = 1 if wkrnr == 222
replace district_outcome = 1 if wkrnr == 223
replace district_outcome = 1 if wkrnr == 224
replace district_outcome = 1 if wkrnr == 225
replace district_outcome = 1 if wkrnr == 226
replace district_outcome = 1 if wkrnr == 227
replace district_outcome = 1 if wkrnr == 228
replace district_outcome = 1 if wkrnr == 229
replace district_outcome = 1 if wkrnr == 230
replace district_outcome = 1 if wkrnr == 231
replace district_outcome = 1 if wkrnr == 232
replace district_outcome = 1 if wkrnr == 233
replace district_outcome = 1 if wkrnr == 234
replace district_outcome = 1 if wkrnr == 235
replace district_outcome = 1 if wkrnr == 236
replace district_outcome = 1 if wkrnr == 237
replace district_outcome = 1 if wkrnr == 238
replace district_outcome = 1 if wkrnr == 239
replace district_outcome = 1 if wkrnr == 240
replace district_outcome = 1 if wkrnr == 241
replace district_outcome = 1 if wkrnr == 242
replace district_outcome = 1 if wkrnr == 243
replace district_outcome = 1 if wkrnr == 244
replace district_outcome = 1 if wkrnr == 245
replace district_outcome = 1 if wkrnr == 246
replace district_outcome = 1 if wkrnr == 247
replace district_outcome = 1 if wkrnr == 248
replace district_outcome = 1 if wkrnr == 249
replace district_outcome = 1 if wkrnr == 250
replace district_outcome = 1 if wkrnr == 251
replace district_outcome = 1 if wkrnr == 252
replace district_outcome = 1 if wkrnr == 253
replace district_outcome = 1 if wkrnr == 254
replace district_outcome = 1 if wkrnr == 255
replace district_outcome = 1 if wkrnr == 256
replace district_outcome = 1 if wkrnr == 257
replace district_outcome = 1 if wkrnr == 258
replace district_outcome = 1 if wkrnr == 259
replace district_outcome = 1 if wkrnr == 260
replace district_outcome = 1 if wkrnr == 261
replace district_outcome = 1 if wkrnr == 262
replace district_outcome = 1 if wkrnr == 263
replace district_outcome = 1 if wkrnr == 264
replace district_outcome = 1 if wkrnr == 265
replace district_outcome = 1 if wkrnr == 266
replace district_outcome = 1 if wkrnr == 267
replace district_outcome = 1 if wkrnr == 268
replace district_outcome = 1 if wkrnr == 269
replace district_outcome = 1 if wkrnr == 270
replace district_outcome = 1 if wkrnr == 271
replace district_outcome = 1 if wkrnr == 272
replace district_outcome = 1 if wkrnr == 273
replace district_outcome = 1 if wkrnr == 274
replace district_outcome = 1 if wkrnr == 275
replace district_outcome = 1 if wkrnr == 276
replace district_outcome = 1 if wkrnr == 277
replace district_outcome = 1 if wkrnr == 278
replace district_outcome = 1 if wkrnr == 279
replace district_outcome = 1 if wkrnr == 280
replace district_outcome = 4 if wkrnr == 281
replace district_outcome = 1 if wkrnr == 282
replace district_outcome = 1 if wkrnr == 283
replace district_outcome = 1 if wkrnr == 284
replace district_outcome = 1 if wkrnr == 285
replace district_outcome = 1 if wkrnr == 286
replace district_outcome = 1 if wkrnr == 287
replace district_outcome = 1 if wkrnr == 288
replace district_outcome = 1 if wkrnr == 289
replace district_outcome = 1 if wkrnr == 290
replace district_outcome = 1 if wkrnr == 291
replace district_outcome = 1 if wkrnr == 292
replace district_outcome = 1 if wkrnr == 293
replace district_outcome = 1 if wkrnr == 294
replace district_outcome = 1 if wkrnr == 295
replace district_outcome = 1 if wkrnr == 296
replace district_outcome = 1 if wkrnr == 297
replace district_outcome = 1 if wkrnr == 298
replace district_outcome = 1 if wkrnr == 299


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 6.06361 if wkrnr == 1
replace margin = 15.31659 if wkrnr == 2
replace margin = 10.00384 if wkrnr == 3
replace margin = 7.68412 if wkrnr == 4
replace margin = 8.212418 if wkrnr == 5
replace margin = 5.17569 if wkrnr == 6
replace margin = 8.974329 if wkrnr == 7
replace margin = 9.186338 if wkrnr == 8
replace margin = 4.377747 if wkrnr == 9
replace margin = 10.01835 if wkrnr == 10
replace margin = 6.315443 if wkrnr == 11
replace margin = 3.691391 if wkrnr == 12
replace margin = .805006 if wkrnr == 13
replace margin = 5.464504 if wkrnr == 14
replace margin = 22.95705 if wkrnr == 15
replace margin = 8.86754 if wkrnr == 16
replace margin = 7.350571 if wkrnr == 17
replace margin = .2172432 if wkrnr == 18
replace margin = 8.038033 if wkrnr == 19
replace margin = 5.848013 if wkrnr == 20
replace margin = 5.135159 if wkrnr == 21
replace margin = 5.213692 if wkrnr == 22
replace margin = 1.688717 if wkrnr == 23
replace margin = 6.153564 if wkrnr == 24
replace margin = 18.52874 if wkrnr == 25
replace margin = 15.97673 if wkrnr == 26
replace margin = 7.409401 if wkrnr == 27
replace margin = 4.212048 if wkrnr == 28
replace margin = .5632362 if wkrnr == 29
replace margin = .875885 if wkrnr == 30
replace margin = 10.81297 if wkrnr == 31
replace margin = 28.61575 if wkrnr == 32
replace margin = 43.22101 if wkrnr == 33
replace margin = 3.057236 if wkrnr == 34
replace margin = .4768219 if wkrnr == 35
replace margin = 4.947647 if wkrnr == 36
replace margin = 9.973322 if wkrnr == 37
replace margin = 2.171516 if wkrnr == 38
replace margin = 11.99728 if wkrnr == 39
replace margin = 5.307224 if wkrnr == 40
replace margin = 4.188663 if wkrnr == 41
replace margin = 6.140369 if wkrnr == 42
replace margin = 7.582127 if wkrnr == 43
replace margin = .4996948 if wkrnr == 44
replace margin = 11.90169 if wkrnr == 45
replace margin = 3.48291 if wkrnr == 46
replace margin = 4.294872 if wkrnr == 47
replace margin = 4.220791 if wkrnr == 48
replace margin = 2.52784 if wkrnr == 49
replace margin = 12.32158 if wkrnr == 50
replace margin = 4.332478 if wkrnr == 51
replace margin = 4.781872 if wkrnr == 52
replace margin = 3.682701 if wkrnr == 53
replace margin = 2.422577 if wkrnr == 54
replace margin = 5.027691 if wkrnr == 55
replace margin = 12.9434 if wkrnr == 56
replace margin = 1.276066 if wkrnr == 57
replace margin = 4.992975 if wkrnr == 58
replace margin = 1.520346 if wkrnr == 59
replace margin = 13.40527 if wkrnr == 60
replace margin = 4.389814 if wkrnr == 61
replace margin = .1207027 if wkrnr == 62
replace margin = 6.096983 if wkrnr == 63
replace margin = 3.798407 if wkrnr == 64
replace margin = 2.17407 if wkrnr == 65
replace margin = .616724 if wkrnr == 66
replace margin = 2.181511 if wkrnr == 67
replace margin = 3.952288 if wkrnr == 68
replace margin = 1.897949 if wkrnr == 69
replace margin = 1.058556 if wkrnr == 70
replace margin = 5.485802 if wkrnr == 71
replace margin = .261301 if wkrnr == 72
replace margin = 2.797783 if wkrnr == 73
replace margin = 1.337143 if wkrnr == 74
replace margin = 3.019154 if wkrnr == 75
replace margin = 3.968424 if wkrnr == 76
replace margin = 1.409382 if wkrnr == 77
replace margin = 11.59445 if wkrnr == 78
replace margin = 3.187527 if wkrnr == 79
replace margin = 11.97756 if wkrnr == 80
replace margin = 1.889219 if wkrnr == 81
replace margin = 6.177868 if wkrnr == 82
replace margin = 3.457706 if wkrnr == 83
replace margin = 29.12457 if wkrnr == 84
replace margin = 24.02357 if wkrnr == 85
replace margin = 28.22209 if wkrnr == 86
replace margin = 29.00455 if wkrnr == 87
replace margin = 9.469091 if wkrnr == 88
replace margin = 5.888012 if wkrnr == 89
replace margin = 26.3096 if wkrnr == 90
replace margin = 15.54046 if wkrnr == 91
replace margin = 3.583183 if wkrnr == 92
replace margin = 14.62803 if wkrnr == 93
replace margin = 1.39114 if wkrnr == 94
replace margin = 2.47588 if wkrnr == 95
replace margin = 7.186747 if wkrnr == 96
replace margin = 2.159031 if wkrnr == 97
replace margin = 19.04436 if wkrnr == 98
replace margin = 25.58591 if wkrnr == 99
replace margin = 20.64094 if wkrnr == 100
replace margin = 23.34208 if wkrnr == 101
replace margin = 1.57531 if wkrnr == 102
replace margin = 1.509918 if wkrnr == 103
replace margin = 4.658356 if wkrnr == 104
replace margin = 10.72507 if wkrnr == 105
replace margin = 4.212166 if wkrnr == 106
replace margin = 14.95057 if wkrnr == 107
replace margin = 4.460201 if wkrnr == 108
replace margin = 18.36311 if wkrnr == 109
replace margin = 17.91596 if wkrnr == 110
replace margin = 13.2076 if wkrnr == 111
replace margin = 23.19801 if wkrnr == 112
replace margin = 19.31233 if wkrnr == 113
replace margin = .2347488 if wkrnr == 114
replace margin = 4.479362 if wkrnr == 115
replace margin = 10.56376 if wkrnr == 116
replace margin = 20.94624 if wkrnr == 117
replace margin = 17.23718 if wkrnr == 118
replace margin = 9.280533 if wkrnr == 119
replace margin = 18.84585 if wkrnr == 120
replace margin = 2.563477 if wkrnr == 121
replace margin = 12.37663 if wkrnr == 122
replace margin = 10.79963 if wkrnr == 123
replace margin = 28.24039 if wkrnr == 124
replace margin = 14.38764 if wkrnr == 125
replace margin = 9.896465 if wkrnr == 126
replace margin = 28.67322 if wkrnr == 127
replace margin = 24.67982 if wkrnr == 128
replace margin = 1.948009 if wkrnr == 129
replace margin = 6.637589 if wkrnr == 130
replace margin = 16.40883 if wkrnr == 131
replace margin = 12.45557 if wkrnr == 132
replace margin = 1.805798 if wkrnr == 133
replace margin = .8609161 if wkrnr == 134
replace margin = 2.882004 if wkrnr == 135
replace margin = 3.367558 if wkrnr == 136
replace margin = 14.56106 if wkrnr == 137
replace margin = 26.78838 if wkrnr == 138
replace margin = 9.465351 if wkrnr == 139
replace margin = 10.49853 if wkrnr == 140
replace margin = 12.35957 if wkrnr == 141
replace margin = 24.41761 if wkrnr == 142
replace margin = 13.68026 if wkrnr == 143
replace margin = 13.26937 if wkrnr == 144
replace margin = 10.15005 if wkrnr == 145
replace margin = 11.11946 if wkrnr == 146
replace margin = 16.64245 if wkrnr == 147
replace margin = 25.06536 if wkrnr == 148
replace margin = 2.404163 if wkrnr == 149
replace margin = 19.05809 if wkrnr == 150
replace margin = 4.662598 if wkrnr == 151
replace margin = 15.19524 if wkrnr == 152
replace margin = 6.782188 if wkrnr == 153
replace margin = 3.493206 if wkrnr == 154
replace margin = 19.11284 if wkrnr == 155
replace margin = 23.84777 if wkrnr == 156
replace margin = 17.15387 if wkrnr == 157
replace margin = 18.06418 if wkrnr == 158
replace margin = 24.71678 if wkrnr == 159
replace margin = 12.60784 if wkrnr == 160
replace margin = 16.89114 if wkrnr == 161
replace margin = 20.39308 if wkrnr == 162
replace margin = 6.282742 if wkrnr == 163
replace margin = 15.96327 if wkrnr == 164
replace margin = 16.6134 if wkrnr == 165
replace margin = 10.70997 if wkrnr == 166
replace margin = 15.9678 if wkrnr == 167
replace margin = 2.425423 if wkrnr == 168
replace margin = 7.640055 if wkrnr == 169
replace margin = 5.729694 if wkrnr == 170
replace margin = 6.756649 if wkrnr == 171
replace margin = 3.302963 if wkrnr == 172
replace margin = 8.503395 if wkrnr == 173
replace margin = 2.539856 if wkrnr == 174
replace margin = 25.84226 if wkrnr == 175
replace margin = 19.80976 if wkrnr == 176
replace margin = 7.947781 if wkrnr == 177
replace margin = 18.45807 if wkrnr == 178
replace margin = 8.138054 if wkrnr == 179
replace margin = 5.323227 if wkrnr == 180
replace margin = 23.57399 if wkrnr == 181
replace margin = 5.160046 if wkrnr == 182
replace margin = 6.468153 if wkrnr == 183
replace margin = .3495445 if wkrnr == 184
replace margin = 11.45279 if wkrnr == 185
replace margin = .0260162 if wkrnr == 186
replace margin = 10.73025 if wkrnr == 187
replace margin = 13.47978 if wkrnr == 188
replace margin = 18.46214 if wkrnr == 189
replace margin = 7.156969 if wkrnr == 190
replace margin = 2.46381 if wkrnr == 191
replace margin = 2.199453 if wkrnr == 192
replace margin = 1.930008 if wkrnr == 193
replace margin = 1.694468 if wkrnr == 194
replace margin = 8.098265 if wkrnr == 195
replace margin = .6176796 if wkrnr == 196
replace margin = 1.368883 if wkrnr == 197
replace margin = 2.764523 if wkrnr == 198
replace margin = 20.62358 if wkrnr == 199
replace margin = 16.05004 if wkrnr == 200
replace margin = 24.01277 if wkrnr == 201
replace margin = 18.31644 if wkrnr == 202
replace margin = 22.01472 if wkrnr == 203
replace margin = 19.76912 if wkrnr == 204
replace margin = 13.76615 if wkrnr == 205
replace margin = 5.730516 if wkrnr == 206
replace margin = 1.638813 if wkrnr == 207
replace margin = 5.997662 if wkrnr == 208
replace margin = 19.40105 if wkrnr == 209
replace margin = 1.732082 if wkrnr == 210
replace margin = 12.36555 if wkrnr == 211
replace margin = 12.92856 if wkrnr == 212
replace margin = 45.64454 if wkrnr == 213
replace margin = 31.21362 if wkrnr == 214
replace margin = 29.69023 if wkrnr == 215
replace margin = 29.81816 if wkrnr == 216
replace margin = 41.78644 if wkrnr == 217
replace margin = .9387817 if wkrnr == 218
replace margin = 9.768158 if wkrnr == 219
replace margin = 9.74176 if wkrnr == 220
replace margin = 9.310106 if wkrnr == 221
replace margin = 26.20025 if wkrnr == 222
replace margin = 37.08055 if wkrnr == 223
replace margin = 38.17795 if wkrnr == 224
replace margin = 39.35129 if wkrnr == 225
replace margin = 37.71259 if wkrnr == 226
replace margin = 36.46973 if wkrnr == 227
replace margin = 35.43362 if wkrnr == 228
replace margin = 27.59418 if wkrnr == 229
replace margin = 36.10485 if wkrnr == 230
replace margin = 32.99216 if wkrnr == 231
replace margin = 26.97862 if wkrnr == 232
replace margin = 23.92278 if wkrnr == 233
replace margin = 27.13206 if wkrnr == 234
replace margin = 25.07224 if wkrnr == 235
replace margin = 28.63373 if wkrnr == 236
replace margin = 30.20512 if wkrnr == 237
replace margin = 23.04961 if wkrnr == 238
replace margin = 19.24314 if wkrnr == 239
replace margin = 53.36952 if wkrnr == 240
replace margin = 25.44088 if wkrnr == 241
replace margin = 21.12557 if wkrnr == 242
replace margin = 18.25632 if wkrnr == 243
replace margin = 5.762005 if wkrnr == 244
replace margin = 8.416126 if wkrnr == 245
replace margin = 20.62315 if wkrnr == 246
replace margin = 23.71884 if wkrnr == 247
replace margin = 35.81525 if wkrnr == 248
replace margin = 32.49085 if wkrnr == 249
replace margin = 26.90776 if wkrnr == 250
replace margin = 20.70052 if wkrnr == 251
replace margin = 22.64027 if wkrnr == 252
replace margin = 37.28716 if wkrnr == 253
replace margin = 36.67484 if wkrnr == 254
replace margin = 35.2613 if wkrnr == 255
replace margin = 40.22213 if wkrnr == 256
replace margin = 37.41508 if wkrnr == 257
replace margin = 4.541166 if wkrnr == 258
replace margin = 8.219187 if wkrnr == 259
replace margin = 25.10003 if wkrnr == 260
replace margin = 14.88369 if wkrnr == 261
replace margin = 19.14921 if wkrnr == 262
replace margin = 16.56022 if wkrnr == 263
replace margin = 15.747 if wkrnr == 264
replace margin = 17.32275 if wkrnr == 265
replace margin = 18.56499 if wkrnr == 266
replace margin = 19.20719 if wkrnr == 267
replace margin = 21.72071 if wkrnr == 268
replace margin = 19.81514 if wkrnr == 269
replace margin = 19.55615 if wkrnr == 270
replace margin = 11.32722 if wkrnr == 271
replace margin = 19.62224 if wkrnr == 272
replace margin = 24.74657 if wkrnr == 273
replace margin = 6.401714 if wkrnr == 274
replace margin = 6.282104 if wkrnr == 275
replace margin = 30.25594 if wkrnr == 276
replace margin = 17.21956 if wkrnr == 277
replace margin = 23.29412 if wkrnr == 278
replace margin = 17.85492 if wkrnr == 279
replace margin = 26.12141 if wkrnr == 280
replace margin = 4.152914 if wkrnr == 281
replace margin = 5.649818 if wkrnr == 282
replace margin = 17.87768 if wkrnr == 283
replace margin = 27.63589 if wkrnr == 284
replace margin = 30.31728 if wkrnr == 285
replace margin = 28.27353 if wkrnr == 286
replace margin = 21.50553 if wkrnr == 287
replace margin = 12.98878 if wkrnr == 288
replace margin = 20.61035 if wkrnr == 289
replace margin = 17.38075 if wkrnr == 290
replace margin = 20.15277 if wkrnr == 291
replace margin = 19.63324 if wkrnr == 292
replace margin = 25.38473 if wkrnr == 293
replace margin = 27.08356 if wkrnr == 294
replace margin = 32.36263 if wkrnr == 295
replace margin = 1.396391 if wkrnr == 296
replace margin = 4.565052 if wkrnr == 297
replace margin = 9.222124 if wkrnr == 298
replace margin = 2.495899 if wkrnr == 299

* Effective number of electoral parties by district

gen enep = .

replace enep = 3.52871 if wkrnr == 1
replace enep = 3.428295 if wkrnr == 2
replace enep = 3.614717 if wkrnr == 3
replace enep = 3.447185 if wkrnr == 4
replace enep = 3.718538 if wkrnr == 5
replace enep = 3.513469 if wkrnr == 6
replace enep = 3.441393 if wkrnr == 7
replace enep = 3.583411 if wkrnr == 8
replace enep = 3.451661 if wkrnr == 9
replace enep = 3.612365 if wkrnr == 10
replace enep = 3.839642 if wkrnr == 11
replace enep = 3.920461 if wkrnr == 12
replace enep = 4.121801 if wkrnr == 13
replace enep = 4.306686 if wkrnr == 14
replace enep = 3.0161 if wkrnr == 15
replace enep = 3.86166 if wkrnr == 16
replace enep = 3.97862 if wkrnr == 17
replace enep = 3.935059 if wkrnr == 18
replace enep = 4.156927 if wkrnr == 19
replace enep = 3.905434 if wkrnr == 20
replace enep = 4.218341 if wkrnr == 21
replace enep = 3.501563 if wkrnr == 22
replace enep = 3.595789 if wkrnr == 23
replace enep = 3.519035 if wkrnr == 24
replace enep = 3.431059 if wkrnr == 25
replace enep = 3.2208 if wkrnr == 26
replace enep = 3.478645 if wkrnr == 27
replace enep = 3.850703 if wkrnr == 28
replace enep = 3.692777 if wkrnr == 29
replace enep = 3.25757 if wkrnr == 30
replace enep = 3.120402 if wkrnr == 31
replace enep = 2.644685 if wkrnr == 32
replace enep = 2.294353 if wkrnr == 33
replace enep = 3.512854 if wkrnr == 34
replace enep = 3.427242 if wkrnr == 35
replace enep = 3.293427 if wkrnr == 36
replace enep = 3.527365 if wkrnr == 37
replace enep = 4.04615 if wkrnr == 38
replace enep = 3.15814 if wkrnr == 39
replace enep = 3.54197 if wkrnr == 40
replace enep = 3.097952 if wkrnr == 41
replace enep = 3.535965 if wkrnr == 42
replace enep = 3.503772 if wkrnr == 43
replace enep = 3.206212 if wkrnr == 44
replace enep = 3.179675 if wkrnr == 45
replace enep = 3.185194 if wkrnr == 46
replace enep = 3.330483 if wkrnr == 47
replace enep = 3.232963 if wkrnr == 48
replace enep = 3.302185 if wkrnr == 49
replace enep = 3.109375 if wkrnr == 50
replace enep = 3.490846 if wkrnr == 51
replace enep = 3.422535 if wkrnr == 52
replace enep = 3.351844 if wkrnr == 53
replace enep = 3.558637 if wkrnr == 54
replace enep = 4.107145 if wkrnr == 55
replace enep = 3.932482 if wkrnr == 56
replace enep = 3.929781 if wkrnr == 57
replace enep = 4.008315 if wkrnr == 58
replace enep = 4.295676 if wkrnr == 59
replace enep = 3.962372 if wkrnr == 60
replace enep = 3.910975 if wkrnr == 61
replace enep = 4.230703 if wkrnr == 62
replace enep = 4.067789 if wkrnr == 63
replace enep = 4.061097 if wkrnr == 64
replace enep = 4.219096 if wkrnr == 65
replace enep = 4.102507 if wkrnr == 66
replace enep = 3.907997 if wkrnr == 67
replace enep = 3.986648 if wkrnr == 68
replace enep = 3.900218 if wkrnr == 69
replace enep = 3.927388 if wkrnr == 70
replace enep = 3.781561 if wkrnr == 71
replace enep = 3.951337 if wkrnr == 72
replace enep = 4.005283 if wkrnr == 73
replace enep = 3.912644 if wkrnr == 74
replace enep = 3.756132 if wkrnr == 75
replace enep = 4.900477 if wkrnr == 76
replace enep = 4.558337 if wkrnr == 77
replace enep = 3.880768 if wkrnr == 78
replace enep = 3.751099 if wkrnr == 79
replace enep = 3.846075 if wkrnr == 80
replace enep = 4.107265 if wkrnr == 81
replace enep = 4.19496 if wkrnr == 82
replace enep = 4.639987 if wkrnr == 83
replace enep = 3.421504 if wkrnr == 84
replace enep = 3.512403 if wkrnr == 85
replace enep = 3.348368 if wkrnr == 86
replace enep = 3.36627 if wkrnr == 87
replace enep = 3.614607 if wkrnr == 88
replace enep = 3.349016 if wkrnr == 89
replace enep = 3.017654 if wkrnr == 90
replace enep = 3.075326 if wkrnr == 91
replace enep = 3.309396 if wkrnr == 92
replace enep = 3.353259 if wkrnr == 93
replace enep = 3.763592 if wkrnr == 94
replace enep = 3.765984 if wkrnr == 95
replace enep = 3.921602 if wkrnr == 96
replace enep = 3.878058 if wkrnr == 97
replace enep = 3.402137 if wkrnr == 98
replace enep = 2.992054 if wkrnr == 99
replace enep = 3.047238 if wkrnr == 100
replace enep = 2.957926 if wkrnr == 101
replace enep = 3.510627 if wkrnr == 102
replace enep = 3.670226 if wkrnr == 103
replace enep = 3.436496 if wkrnr == 104
replace enep = 3.062375 if wkrnr == 105
replace enep = 3.285398 if wkrnr == 106
replace enep = 3.376954 if wkrnr == 107
replace enep = 3.585146 if wkrnr == 108
replace enep = 3.018128 if wkrnr == 109
replace enep = 3.269996 if wkrnr == 110
replace enep = 3.458663 if wkrnr == 111
replace enep = 3.076388 if wkrnr == 112
replace enep = 2.914121 if wkrnr == 113
replace enep = 3.227179 if wkrnr == 114
replace enep = 3.340977 if wkrnr == 115
replace enep = 3.347168 if wkrnr == 116
replace enep = 3.185726 if wkrnr == 117
replace enep = 3.359262 if wkrnr == 118
replace enep = 3.378293 if wkrnr == 119
replace enep = 3.260929 if wkrnr == 120
replace enep = 3.346742 if wkrnr == 121
replace enep = 3.326677 if wkrnr == 122
replace enep = 3.30929 if wkrnr == 123
replace enep = 2.66797 if wkrnr == 124
replace enep = 3.204451 if wkrnr == 125
replace enep = 3.242943 if wkrnr == 126
replace enep = 2.68408 if wkrnr == 127
replace enep = 2.907668 if wkrnr == 128
replace enep = 2.708988 if wkrnr == 129
replace enep = 3.458738 if wkrnr == 130
replace enep = 2.752204 if wkrnr == 131
replace enep = 3.109295 if wkrnr == 132
replace enep = 3.568062 if wkrnr == 133
replace enep = 3.230449 if wkrnr == 134
replace enep = 2.873776 if wkrnr == 135
replace enep = 3.165123 if wkrnr == 136
replace enep = 3.064396 if wkrnr == 137
replace enep = 2.843257 if wkrnr == 138
replace enep = 3.190126 if wkrnr == 139
replace enep = 3.519324 if wkrnr == 140
replace enep = 3.297185 if wkrnr == 141
replace enep = 2.854265 if wkrnr == 142
replace enep = 3.647177 if wkrnr == 143
replace enep = 3.470001 if wkrnr == 144
replace enep = 3.275989 if wkrnr == 145
replace enep = 3.161115 if wkrnr == 146
replace enep = 3.178456 if wkrnr == 147
replace enep = 2.831871 if wkrnr == 148
replace enep = 2.924406 if wkrnr == 149
replace enep = 3.08831 if wkrnr == 150
replace enep = 3.184216 if wkrnr == 151
replace enep = 3.717136 if wkrnr == 152
replace enep = 4.278006 if wkrnr == 153
replace enep = 4.527902 if wkrnr == 154
replace enep = 3.770507 if wkrnr == 155
replace enep = 3.572564 if wkrnr == 156
replace enep = 3.663072 if wkrnr == 157
replace enep = 3.742268 if wkrnr == 158
replace enep = 3.624534 if wkrnr == 159
replace enep = 4.224751 if wkrnr == 160
replace enep = 4.49623 if wkrnr == 161
replace enep = 3.590228 if wkrnr == 162
replace enep = 4.044735 if wkrnr == 163
replace enep = 3.700885 if wkrnr == 164
replace enep = 3.876999 if wkrnr == 165
replace enep = 3.804371 if wkrnr == 166
replace enep = 3.754658 if wkrnr == 167
replace enep = 3.456047 if wkrnr == 168
replace enep = 3.732433 if wkrnr == 169
replace enep = 3.311512 if wkrnr == 170
replace enep = 3.376465 if wkrnr == 171
replace enep = 3.357218 if wkrnr == 172
replace enep = 3.322611 if wkrnr == 173
replace enep = 3.610313 if wkrnr == 174
replace enep = 3.071451 if wkrnr == 175
replace enep = 3.393112 if wkrnr == 176
replace enep = 3.389371 if wkrnr == 177
replace enep = 3.218972 if wkrnr == 178
replace enep = 3.409026 if wkrnr == 179
replace enep = 3.466523 if wkrnr == 180
replace enep = 3.214247 if wkrnr == 181
replace enep = 4.037498 if wkrnr == 182
replace enep = 3.970385 if wkrnr == 183
replace enep = 3.528618 if wkrnr == 184
replace enep = 3.694185 if wkrnr == 185
replace enep = 3.692618 if wkrnr == 186
replace enep = 3.622004 if wkrnr == 187
replace enep = 3.213499 if wkrnr == 188
replace enep = 3.554504 if wkrnr == 189
replace enep = 3.916834 if wkrnr == 190
replace enep = 3.91392 if wkrnr == 191
replace enep = 4.260522 if wkrnr == 192
replace enep = 4.180056 if wkrnr == 193
replace enep = 4.255572 if wkrnr == 194
replace enep = 3.763809 if wkrnr == 195
replace enep = 4.066383 if wkrnr == 196
replace enep = 3.96715 if wkrnr == 197
replace enep = 3.273747 if wkrnr == 198
replace enep = 3.323209 if wkrnr == 199
replace enep = 3.358557 if wkrnr == 200
replace enep = 3.218397 if wkrnr == 201
replace enep = 3.12121 if wkrnr == 202
replace enep = 3.325904 if wkrnr == 203
replace enep = 3.317366 if wkrnr == 204
replace enep = 3.376717 if wkrnr == 205
replace enep = 3.863585 if wkrnr == 206
replace enep = 3.431153 if wkrnr == 207
replace enep = 3.677727 if wkrnr == 208
replace enep = 3.501215 if wkrnr == 209
replace enep = 3.836453 if wkrnr == 210
replace enep = 3.808833 if wkrnr == 211
replace enep = 3.686781 if wkrnr == 212
replace enep = 2.452043 if wkrnr == 213
replace enep = 3.295923 if wkrnr == 214
replace enep = 3.437973 if wkrnr == 215
replace enep = 3.314166 if wkrnr == 216
replace enep = 2.708195 if wkrnr == 217
replace enep = 3.543636 if wkrnr == 218
replace enep = 4.113023 if wkrnr == 219
replace enep = 3.847787 if wkrnr == 220
replace enep = 4.008437 if wkrnr == 221
replace enep = 3.512268 if wkrnr == 222
replace enep = 3.199788 if wkrnr == 223
replace enep = 2.853224 if wkrnr == 224
replace enep = 2.920287 if wkrnr == 225
replace enep = 3.137136 if wkrnr == 226
replace enep = 3.00793 if wkrnr == 227
replace enep = 3.218518 if wkrnr == 228
replace enep = 3.50505 if wkrnr == 229
replace enep = 2.974948 if wkrnr == 230
replace enep = 2.711341 if wkrnr == 231
replace enep = 3.386321 if wkrnr == 232
replace enep = 3.666388 if wkrnr == 233
replace enep = 2.978104 if wkrnr == 234
replace enep = 3.673076 if wkrnr == 235
replace enep = 3.262582 if wkrnr == 236
replace enep = 3.073258 if wkrnr == 237
replace enep = 3.166479 if wkrnr == 238
replace enep = 3.23907 if wkrnr == 239
replace enep = 2.032199 if wkrnr == 240
replace enep = 3.401535 if wkrnr == 241
replace enep = 3.486132 if wkrnr == 242
replace enep = 3.640673 if wkrnr == 243
replace enep = 3.896654 if wkrnr == 244
replace enep = 3.802416 if wkrnr == 245
replace enep = 3.541596 if wkrnr == 246
replace enep = 3.759727 if wkrnr == 247
replace enep = 2.914764 if wkrnr == 248
replace enep = 2.989106 if wkrnr == 249
replace enep = 3.52426 if wkrnr == 250
replace enep = 3.589564 if wkrnr == 251
replace enep = 3.91376 if wkrnr == 252
replace enep = 3.034664 if wkrnr == 253
replace enep = 3.069287 if wkrnr == 254
replace enep = 3.217818 if wkrnr == 255
replace enep = 3.045813 if wkrnr == 256
replace enep = 3.243291 if wkrnr == 257
replace enep = 3.949486 if wkrnr == 258
replace enep = 4.249755 if wkrnr == 259
replace enep = 3.551981 if wkrnr == 260
replace enep = 3.370973 if wkrnr == 261
replace enep = 3.543119 if wkrnr == 262
replace enep = 3.52213 if wkrnr == 263
replace enep = 3.4997 if wkrnr == 264
replace enep = 3.918301 if wkrnr == 265
replace enep = 3.714739 if wkrnr == 266
replace enep = 3.503593 if wkrnr == 267
replace enep = 3.690808 if wkrnr == 268
replace enep = 3.471992 if wkrnr == 269
replace enep = 3.422476 if wkrnr == 270
replace enep = 3.958718 if wkrnr == 271
replace enep = 3.367696 if wkrnr == 272
replace enep = 3.219547 if wkrnr == 273
replace enep = 3.84065 if wkrnr == 274
replace enep = 3.885835 if wkrnr == 275
replace enep = 3.143229 if wkrnr == 276
replace enep = 3.638096 if wkrnr == 277
replace enep = 3.314088 if wkrnr == 278
replace enep = 3.855724 if wkrnr == 279
replace enep = 3.505667 if wkrnr == 280
replace enep = 3.992965 if wkrnr == 281
replace enep = 3.64261 if wkrnr == 282
replace enep = 3.644087 if wkrnr == 283
replace enep = 3.395225 if wkrnr == 284
replace enep = 3.333459 if wkrnr == 285
replace enep = 3.376044 if wkrnr == 286
replace enep = 3.632934 if wkrnr == 287
replace enep = 3.554837 if wkrnr == 288
replace enep = 3.701691 if wkrnr == 289
replace enep = 3.951379 if wkrnr == 290
replace enep = 3.698755 if wkrnr == 291
replace enep = 3.686776 if wkrnr == 292
replace enep = 3.524712 if wkrnr == 293
replace enep = 3.596405 if wkrnr == 294
replace enep = 3.27447 if wkrnr == 295
replace enep = 3.960566 if wkrnr == 296
replace enep = 3.554298 if wkrnr == 297
replace enep = 3.446645 if wkrnr == 298
replace enep = 3.946096 if wkrnr == 299

   
*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 0 if wkrnr == 1
replace reelected = 1 if wkrnr == 2
replace reelected = 1 if wkrnr == 3
replace reelected = 1 if wkrnr == 4
replace reelected = 1 if wkrnr == 5
replace reelected = 0 if wkrnr == 6
replace reelected = 1 if wkrnr == 7
replace reelected = 1 if wkrnr == 8
replace reelected = 0 if wkrnr == 9
replace reelected = 1 if wkrnr == 10
replace reelected = 1 if wkrnr == 11
replace reelected = 0 if wkrnr == 12
replace reelected = 0 if wkrnr == 13
replace reelected = 0 if wkrnr == 14
replace reelected = 1 if wkrnr == 15
replace reelected = 1 if wkrnr == 16
replace reelected = 0 if wkrnr == 17
replace reelected = 1 if wkrnr == 18
replace reelected = 1 if wkrnr == 19
replace reelected = 1 if wkrnr == 20
replace reelected = 0 if wkrnr == 21
replace reelected = 0 if wkrnr == 22
replace reelected = 0 if wkrnr == 23
replace reelected = 1 if wkrnr == 24
replace reelected = 1 if wkrnr == 25
replace reelected = 1 if wkrnr == 26
replace reelected = 1 if wkrnr == 27
replace reelected = 0 if wkrnr == 28
replace reelected = 0 if wkrnr == 29
replace reelected = 0 if wkrnr == 30
replace reelected = 0 if wkrnr == 31
replace reelected = 1 if wkrnr == 32
replace reelected = 1 if wkrnr == 33
replace reelected = 0 if wkrnr == 34
replace reelected = 0 if wkrnr == 35
replace reelected = 0 if wkrnr == 36
replace reelected = 1 if wkrnr == 37
replace reelected = 0 if wkrnr == 38
replace reelected = 1 if wkrnr == 39
replace reelected = 0 if wkrnr == 40
replace reelected = 1 if wkrnr == 41
replace reelected = 1 if wkrnr == 42
replace reelected = 1 if wkrnr == 43
replace reelected = 1 if wkrnr == 44
replace reelected = 0 if wkrnr == 45
replace reelected = 1 if wkrnr == 46
replace reelected = 1 if wkrnr == 47
replace reelected = 1 if wkrnr == 48
replace reelected = 1 if wkrnr == 49
replace reelected = 1 if wkrnr == 50
replace reelected = 1 if wkrnr == 51
replace reelected = 0 if wkrnr == 52
replace reelected = 1 if wkrnr == 53
replace reelected = 1 if wkrnr == 54
replace reelected = 1 if wkrnr == 55
replace reelected = 1 if wkrnr == 56
replace reelected = 1 if wkrnr == 57
replace reelected = 0 if wkrnr == 58
replace reelected = 1 if wkrnr == 59
replace reelected = 0 if wkrnr == 60
replace reelected = 1 if wkrnr == 61
replace reelected = 1 if wkrnr == 62
replace reelected = 1 if wkrnr == 63
replace reelected = 0 if wkrnr == 64
replace reelected = 0 if wkrnr == 65
replace reelected = 0 if wkrnr == 66
replace reelected = 0 if wkrnr == 67
replace reelected = 0 if wkrnr == 68
replace reelected = 0 if wkrnr == 69
replace reelected = 0 if wkrnr == 70
replace reelected = 0 if wkrnr == 71
replace reelected = 0 if wkrnr == 72
replace reelected = 0 if wkrnr == 73
replace reelected = 0 if wkrnr == 74
replace reelected = 0 if wkrnr == 75
replace reelected = 1 if wkrnr == 76
replace reelected = 0 if wkrnr == 77
replace reelected = 0 if wkrnr == 78
replace reelected = 0 if wkrnr == 79
replace reelected = 1 if wkrnr == 80
replace reelected = 1 if wkrnr == 81
replace reelected = 0 if wkrnr == 82
replace reelected = 0 if wkrnr == 83
replace reelected = 1 if wkrnr == 84
replace reelected = 1 if wkrnr == 85
replace reelected = 1 if wkrnr == 86
replace reelected = 1 if wkrnr == 87
replace reelected = 0 if wkrnr == 88
replace reelected = 0 if wkrnr == 89
replace reelected = 1 if wkrnr == 90
replace reelected = 1 if wkrnr == 91
replace reelected = 0 if wkrnr == 92
replace reelected = 1 if wkrnr == 93
replace reelected = 1 if wkrnr == 94
replace reelected = 0 if wkrnr == 95
replace reelected = 1 if wkrnr == 96
replace reelected = 1 if wkrnr == 97
replace reelected = 1 if wkrnr == 98
replace reelected = 1 if wkrnr == 99
replace reelected = 1 if wkrnr == 100
replace reelected = 1 if wkrnr == 101
replace reelected = 1 if wkrnr == 102
replace reelected = 1 if wkrnr == 103
replace reelected = 0 if wkrnr == 104
replace reelected = 1 if wkrnr == 105
replace reelected = 0 if wkrnr == 106
replace reelected = 1 if wkrnr == 107
replace reelected = 0 if wkrnr == 108
replace reelected = 1 if wkrnr == 109
replace reelected = 1 if wkrnr == 110
replace reelected = 1 if wkrnr == 111
replace reelected = 1 if wkrnr == 112
replace reelected = 1 if wkrnr == 113
replace reelected = 0 if wkrnr == 114
replace reelected = 1 if wkrnr == 115
replace reelected = 1 if wkrnr == 116
replace reelected = 1 if wkrnr == 117
replace reelected = 1 if wkrnr == 118
replace reelected = 1 if wkrnr == 119
replace reelected = 1 if wkrnr == 120
replace reelected = 1 if wkrnr == 121
replace reelected = 1 if wkrnr == 122
replace reelected = 1 if wkrnr == 123
replace reelected = 1 if wkrnr == 124
replace reelected = 1 if wkrnr == 125
replace reelected = 1 if wkrnr == 126
replace reelected = 1 if wkrnr == 127
replace reelected = 1 if wkrnr == 128
replace reelected = 0 if wkrnr == 129
replace reelected = 0 if wkrnr == 130
replace reelected = 1 if wkrnr == 131
replace reelected = 1 if wkrnr == 132
replace reelected = 0 if wkrnr == 133
replace reelected = 1 if wkrnr == 134
replace reelected = 0 if wkrnr == 135
replace reelected = 1 if wkrnr == 136
replace reelected = 1 if wkrnr == 137
replace reelected = 1 if wkrnr == 138
replace reelected = 1 if wkrnr == 139
replace reelected = 1 if wkrnr == 140
replace reelected = 1 if wkrnr == 141
replace reelected = 1 if wkrnr == 142
replace reelected = 1 if wkrnr == 143
replace reelected = 1 if wkrnr == 144
replace reelected = 1 if wkrnr == 145
replace reelected = 1 if wkrnr == 146
replace reelected = 1 if wkrnr == 147
replace reelected = 1 if wkrnr == 148
replace reelected = 0 if wkrnr == 149
replace reelected = 1 if wkrnr == 150
replace reelected = 1 if wkrnr == 151
replace reelected = 1 if wkrnr == 152
replace reelected = 0 if wkrnr == 153
replace reelected = 0 if wkrnr == 154
replace reelected = 1 if wkrnr == 155
replace reelected = 1 if wkrnr == 156
replace reelected = 1 if wkrnr == 157
replace reelected = 1 if wkrnr == 158
replace reelected = 1 if wkrnr == 159
replace reelected = 1 if wkrnr == 160
replace reelected = 1 if wkrnr == 161
replace reelected = 1 if wkrnr == 162
replace reelected = 0 if wkrnr == 163
replace reelected = 1 if wkrnr == 164
replace reelected = 1 if wkrnr == 165
replace reelected = 1 if wkrnr == 166
replace reelected = 1 if wkrnr == 167
replace reelected = 1 if wkrnr == 168
replace reelected = 1 if wkrnr == 169
replace reelected = 1 if wkrnr == 170
replace reelected = 1 if wkrnr == 171
replace reelected = 1 if wkrnr == 172
replace reelected = 0 if wkrnr == 173
replace reelected = 0 if wkrnr == 174
replace reelected = 1 if wkrnr == 175
replace reelected = 1 if wkrnr == 176
replace reelected = 0 if wkrnr == 177
replace reelected = 1 if wkrnr == 178
replace reelected = 0 if wkrnr == 179
replace reelected = 0 if wkrnr == 180
replace reelected = 1 if wkrnr == 181
replace reelected = 0 if wkrnr == 182
replace reelected = 1 if wkrnr == 183
replace reelected = 0 if wkrnr == 184
replace reelected = 1 if wkrnr == 185
replace reelected = 1 if wkrnr == 186
replace reelected = 1 if wkrnr == 187
replace reelected = 1 if wkrnr == 188
replace reelected = 1 if wkrnr == 189
replace reelected = 0 if wkrnr == 190
replace reelected = 1 if wkrnr == 191
replace reelected = 0 if wkrnr == 192
replace reelected = 0 if wkrnr == 193
replace reelected = 0 if wkrnr == 194
replace reelected = 1 if wkrnr == 195
replace reelected = 0 if wkrnr == 196
replace reelected = 0 if wkrnr == 197
replace reelected = 0 if wkrnr == 198
replace reelected = 1 if wkrnr == 199
replace reelected = 1 if wkrnr == 200
replace reelected = 1 if wkrnr == 201
replace reelected = 1 if wkrnr == 202
replace reelected = 1 if wkrnr == 203
replace reelected = 1 if wkrnr == 204
replace reelected = 1 if wkrnr == 205
replace reelected = 0 if wkrnr == 206
replace reelected = 1 if wkrnr == 207
replace reelected = 0 if wkrnr == 208
replace reelected = 1 if wkrnr == 209
replace reelected = 1 if wkrnr == 210
replace reelected = 1 if wkrnr == 211
replace reelected = 1 if wkrnr == 212
replace reelected = 1 if wkrnr == 213
replace reelected = 1 if wkrnr == 214
replace reelected = 1 if wkrnr == 215
replace reelected = 1 if wkrnr == 216
replace reelected = 1 if wkrnr == 217
replace reelected = 0 if wkrnr == 218
replace reelected = 1 if wkrnr == 219
replace reelected = 1 if wkrnr == 220
replace reelected = 1 if wkrnr == 221
replace reelected = 1 if wkrnr == 222
replace reelected = 1 if wkrnr == 223
replace reelected = 1 if wkrnr == 224
replace reelected = 1 if wkrnr == 225
replace reelected = 1 if wkrnr == 226
replace reelected = 1 if wkrnr == 227
replace reelected = 1 if wkrnr == 228
replace reelected = 1 if wkrnr == 229
replace reelected = 1 if wkrnr == 230
replace reelected = 1 if wkrnr == 231
replace reelected = 1 if wkrnr == 232
replace reelected = 1 if wkrnr == 233
replace reelected = 1 if wkrnr == 234
replace reelected = 1 if wkrnr == 235
replace reelected = 1 if wkrnr == 236
replace reelected = 1 if wkrnr == 237
replace reelected = 1 if wkrnr == 238
replace reelected = 1 if wkrnr == 239
replace reelected = 1 if wkrnr == 240
replace reelected = 1 if wkrnr == 241
replace reelected = 1 if wkrnr == 242
replace reelected = 1 if wkrnr == 243
replace reelected = 1 if wkrnr == 244
replace reelected = 1 if wkrnr == 245
replace reelected = 1 if wkrnr == 246
replace reelected = 1 if wkrnr == 247
replace reelected = 1 if wkrnr == 248
replace reelected = 1 if wkrnr == 249
replace reelected = 1 if wkrnr == 250
replace reelected = 1 if wkrnr == 251
replace reelected = 1 if wkrnr == 252
replace reelected = 1 if wkrnr == 253
replace reelected = 1 if wkrnr == 254
replace reelected = 1 if wkrnr == 255
replace reelected = 1 if wkrnr == 256
replace reelected = 1 if wkrnr == 257
replace reelected = 1 if wkrnr == 258
replace reelected = 0 if wkrnr == 259
replace reelected = 1 if wkrnr == 260
replace reelected = 1 if wkrnr == 261
replace reelected = 1 if wkrnr == 262
replace reelected = 1 if wkrnr == 263
replace reelected = 1 if wkrnr == 264
replace reelected = 1 if wkrnr == 265
replace reelected = 1 if wkrnr == 266
replace reelected = 1 if wkrnr == 267
replace reelected = 1 if wkrnr == 268
replace reelected = 1 if wkrnr == 269
replace reelected = 1 if wkrnr == 270
replace reelected = 1 if wkrnr == 271
replace reelected = 1 if wkrnr == 272
replace reelected = 1 if wkrnr == 273
replace reelected = 1 if wkrnr == 274
replace reelected = 0 if wkrnr == 275
replace reelected = 1 if wkrnr == 276
replace reelected = 1 if wkrnr == 277
replace reelected = 1 if wkrnr == 278
replace reelected = 1 if wkrnr == 279
replace reelected = 1 if wkrnr == 280
replace reelected = 1 if wkrnr == 281
replace reelected = 0 if wkrnr == 282
replace reelected = 1 if wkrnr == 283
replace reelected = 1 if wkrnr == 284
replace reelected = 1 if wkrnr == 285
replace reelected = 1 if wkrnr == 286
replace reelected = 1 if wkrnr == 287
replace reelected = 1 if wkrnr == 288
replace reelected = 1 if wkrnr == 289
replace reelected = 1 if wkrnr == 290
replace reelected = 1 if wkrnr == 291
replace reelected = 1 if wkrnr == 292
replace reelected = 1 if wkrnr == 293
replace reelected = 1 if wkrnr == 294
replace reelected = 1 if wkrnr == 295
replace reelected = 0 if wkrnr == 296
replace reelected = 0 if wkrnr == 297
replace reelected = 0 if wkrnr == 298
replace reelected = 0 if wkrnr == 299


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen sdate = dat

gen surveydate=date(string(sdate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY surveydate

gen edate = 20090927

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = (electiondate - surveydate) - 1 // Substract 1 to correct for election date

* Gender

gen male = vn1

recode male (2=0)

* Age

gen age = vn542

recode age (9999=.)

replace age = . if age < 17

* Education

gen education = vn9a

recode education (9=.)
recode education (97=.)
recode education (99=.)

* Interest for election

gen interest = v283

recode interest (98=.)
recode interest (99=.)
recode interest (102=.)

replace interest = 6 - interest

* Interest in the outcome

gen care_whole = v284

recode care_whole (98=.)
recode care_whole (99=.)
recode care_whole (102=.)

replace care_whole = 6 - care_whole

* Attention to election campaign

gen attention = n100

recode attention (98=.)
recode attention (99=.)
recode attention (102=.)

replace attention = 6 - attention

* Party identification (national) 

gen pid = vn135a

recode pid (2=1)
recode pid (3=1) // CDU and CSU recoded as CDU/CSU

gen pidstatus_whole = .

replace pidstatus_whole = 9999 if pid == .
replace pidstatus_whole = 9999 if pid == 998
replace pidstatus_whole = 9999 if pid == 999
replace pidstatus_whole = 9999 if pid == 1000

replace pidstatus_whole = 2 if vn135a == 808

replace pidstatus_whole = 3 if vn135a == 1 & pidstatus_whole!=2 & pidstatus_whole!=9999 | vn135a == 4 & pidstatus_whole!=2 & pidstatus_whole!=9999

replace pidstatus_whole = 1 if vn135a!=1 & vn135a!=. & vn135a!=4 & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999

replace pidstatus_whole = . if pidstatus_whole == 9999

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID"
label values pidstatus_whole pidstatus

* Party identification scale (national)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & vn136 == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & vn136 == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & vn136 == 3
replace pidscale_whole = 4 if pidstatus_whole == 1 & vn136 == 4
replace pidscale_whole = 5 if pidstatus_whole == 1 & vn136 == 5
replace pidscale_whole = 6 if pidstatus_whole == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & vn136 == 5
replace pidscale_whole = 8 if pidstatus_whole == 3 & vn136 == 4
replace pidscale_whole = 9 if pidstatus_whole == 3 & vn136 == 3
replace pidscale_whole = 10 if pidstatus_whole == 3 & vn136 == 2
replace pidscale_whole = 11 if pidstatus_whole == 3 & vn136 == 1

label define pidscale 1 "Very strong loser PID" 2 "Fairly strong loser PID" 3 "Moderate loser PID" 4 "Fairly weak loser PID" 5 "Very weak loser PID" 6 "No PID" 7 "Very weak winner PID" 8 "Fairly weak winner PID" 9 "Moderate winner PID" 10 "Fairly strong winner PID" 11 "Very strong winner PID"
label values pidscale_whole pidscale

* Party identification (district) 

gen pidstatus_district = .

replace pidstatus_district = 9999 if pid == .
replace pidstatus_district = 9999 if pid == 998
replace pidstatus_district = 9999 if pid == 999
replace pidstatus_district = 9999 if pid == 1000

replace pidstatus_district = 2 if pid == 808

replace pidstatus_district = 3 if pid == district_outcome & pidstatus_district!=2 & pidstatus_district!=9999

replace pidstatus_district = 1 if pid!=district_outcome & vn135a!=. & pidstatus_district!=2 & pidstatus_district!=3 & pidstatus_district!=9999

replace pidstatus_district = . if pidstatus_district == 9999

label values pidstatus_district pidstatus

* Party identification scale (district)

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & vn136 == 1
replace pidscale_district = 2 if pidstatus_district == 1 & vn136 == 2
replace pidscale_district = 3 if pidstatus_district == 1 & vn136 == 3
replace pidscale_district = 4 if pidstatus_district == 1 & vn136 == 4
replace pidscale_district = 5 if pidstatus_district == 1 & vn136 == 5
replace pidscale_district = 6 if pidstatus_district == 2
replace pidscale_district = 7 if pidstatus_district == 3 & vn136 == 5
replace pidscale_district = 8 if pidstatus_district == 3 & vn136 == 4
replace pidscale_district = 9 if pidstatus_district == 3 & vn136 == 3
replace pidscale_district = 10 if pidstatus_district == 3 & vn136 == 2
replace pidscale_district = 11 if pidstatus_district == 3 & vn136 == 1

label values pidscale_district pidscale

* Coalition preference

gen coalprefA = v258a
gen coalprefB = v258b
gen coalprefC = v258c
gen coalprefD = v258d
gen coalprefE = v258e
gen coalprefF = v258f
gen coalprefG = v258g
gen coalprefH = v258h

recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG coalprefH (98=.)
recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG coalprefH (99=.)
recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG coalprefH (102=.)

gen coalpref_complete = 1 if coalprefA!=. & coalprefB!=. & coalprefC!=. & coalprefD!=. & coalprefE!=. & coalprefF!=. & coalprefG!=. & coalprefH!=.

gen coalpref = 3 if coalprefB >= coalprefA & coalprefB >= coalprefC & coalprefB >= coalprefD & coalprefB >= coalprefE & coalprefB >= coalprefF & coalprefB >= coalprefG & coalprefB >= coalprefH & coalpref_complete == 1
replace coalpref = 2 if coalprefA == coalprefB & coalprefA == coalprefC & coalprefA == coalprefD & coalprefA == coalprefE & coalprefA == coalprefF & coalprefA == coalprefG & coalprefA == coalprefH & coalpref_complete == 1
replace coalpref = 1 if coalpref!=2 & coalpref!=3 & coalpref_complete == 1

replace coalpref = . if coalpref == 3 & coalprefB == coalprefA & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefB == coalprefC & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefB == coalprefD & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefB == coalprefE & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefB == coalprefF & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefB == coalprefG & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefB == coalprefH & coalpref_complete == 1

label define coalpref 1 "Loser coalition" 2 "No preference" 3 "Winner coalition"
label values coalpref coalpref

* Coalition preference strength

gen coalstrengthA = coalprefA if coalprefA >= coalprefB & coalprefA >= coalprefC & coalprefA >= coalprefD & coalprefA >= coalprefE & coalprefA >= coalprefF & coalprefA >= coalprefG & coalprefA >= coalprefH & coalpref_complete == 1
gen coalstrengthB = coalprefB if coalprefB >= coalprefA & coalprefB >= coalprefC & coalprefB >= coalprefD & coalprefB >= coalprefE & coalprefB >= coalprefF & coalprefB >= coalprefG & coalprefB >= coalprefH & coalpref_complete == 1
gen coalstrengthC = coalprefC if coalprefC >= coalprefA & coalprefC >= coalprefB & coalprefC >= coalprefD & coalprefC >= coalprefE & coalprefC >= coalprefF & coalprefC >= coalprefG & coalprefC >= coalprefH & coalpref_complete == 1
gen coalstrengthD = coalprefD if coalprefD >= coalprefA & coalprefD >= coalprefB & coalprefD >= coalprefC & coalprefD >= coalprefE & coalprefD >= coalprefF & coalprefD >= coalprefG & coalprefD >= coalprefH & coalpref_complete == 1
gen coalstrengthE = coalprefE if coalprefE >= coalprefA & coalprefE >= coalprefB & coalprefE >= coalprefC & coalprefE >= coalprefD & coalprefE >= coalprefF & coalprefE >= coalprefG & coalprefE >= coalprefH & coalpref_complete == 1
gen coalstrengthF = coalprefF if coalprefF >= coalprefA & coalprefF >= coalprefB & coalprefF >= coalprefC & coalprefF >= coalprefD & coalprefF >= coalprefE & coalprefF >= coalprefG & coalprefF >= coalprefH & coalpref_complete == 1
gen coalstrengthG = coalprefG if coalprefG >= coalprefA & coalprefG >= coalprefB & coalprefG >= coalprefC & coalprefG >= coalprefD & coalprefG >= coalprefE & coalprefG >= coalprefF & coalprefG >= coalprefH & coalpref_complete == 1
gen coalstrengthH = coalprefH if coalprefH >= coalprefA & coalprefH >= coalprefB & coalprefH >= coalprefC & coalprefH >= coalprefD & coalprefH >= coalprefE & coalprefH >= coalprefF & coalprefH >= coalprefG & coalpref_complete == 1

gen coalstrength = max(coalstrengthA,coalstrengthB,coalstrengthC,coalstrengthD,coalstrengthE,coalstrengthF,coalstrengthG,coalstrengthH)

replace coalstrength = 300 if coalstrength == 8 | coalstrength == 9 | coalstrength == 10 | coalstrength == 11
replace coalstrength = 200 if coalstrength == 5 | coalstrength == 6 | coalstrength == 7
replace coalstrength = 100 if coalstrength == 1 | coalstrength == 2 | coalstrength == 3 | coalstrength == 4

replace coalstrength = 3 if coalstrength == 300
replace coalstrength = 2 if coalstrength == 200
replace coalstrength = 1 if coalstrength == 100

label define coalstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values coalstrength coalstrength

* Coalition preference scale

gen coalscale = .

replace coalscale = 1 if coalpref == 1 & coalstrength == 3
replace coalscale = 2 if coalpref == 1 & coalstrength == 2
replace coalscale = 3 if coalpref == 1 & coalstrength == 1
replace coalscale = 4 if coalpref == 2
replace coalscale = 5 if coalpref == 3 & coalstrength == 1
replace coalscale = 6 if coalpref == 3 & coalstrength == 2
replace coalscale = 7 if coalpref == 3 & coalstrength == 3

label define coalscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values coalscale coalscale

* Political discussion

gen discussion = v211

recode discussion (98=.)
recode discussion (99=.)
recode discussion (102=.)

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.

* Network expertise

gen expertise1 = v214
gen expertise2 = v280

recode expertise1 expertise2 (98=.)
recode expertise1 expertise2 (99=.)
recode expertise1 expertise2 (100=.)
recode expertise1 expertise2 (102=.)

replace expertise1 = 4 - expertise1
replace expertise2 = 4 - expertise2

replace expertise1 = 0 if discussion == 0
replace expertise2 = 0 if discussion == 0 | v277 > 2

gen expertise = expertise1 + expertise2

egen expertise_4pts = rowmax(expertise1 expertise2)

* Political disagreement

gen disagreement1 = v215
gen disagreement2 = v281

recode disagreement1 disagreement2 (98=.)
recode disagreement1 disagreement2 (99=.)
recode disagreement1 disagreement2 (100=.)
recode disagreement1 disagreement2 (102=.)

replace disagreement1 = 4 - disagreement1
replace disagreement2 = 4 - disagreement2

replace disagreement1 = 0 if discussion == 0
replace disagreement2 = 0 if discussion == 0 | v277 > 2

gen disagreement = disagreement1 + disagreement2

gen disagreement1_3pts = disagreement1
gen disagreement2_3pts = disagreement2

replace disagreement1_3pts = 0 if disagreement1 == 1
replace disagreement1_3pts = 1 if disagreement1 == 2
replace disagreement1_3pts = 2 if disagreement1 == 3

replace disagreement2_3pts = 0 if disagreement2 == 1
replace disagreement2_3pts = 1 if disagreement2 == 2
replace disagreement2_3pts = 2 if disagreement2 == 3

egen disagreement_3pts = rowmax(disagreement1_3pts disagreement2_3pts)

* Main source of information

gen network_main = 1 if v197 == 5

replace network_main = 0 if v197!=5
replace network_main = . if v197 == 98
replace network_main = . if v197 == 99
replace network_main = .  if v197 == 102


*********************************
** NATIONAL-LEVEL EXPECTATIONS ** 
*********************************

* Forecasts (1 = CDU/CSU and FDP, 2 = SPD and Bündnis 90/Die Grünen, 3 = none)

gen forecast_whole = v264

recode forecast_whole (99=.) 
recode forecast_whole (102=.) 

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=1 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 1 & forecast_whole!=.


*********************************
** DISTRICT-LEVEL EXPECTATIONS ** 
*********************************

* Forecasts (1 = CDU/CSU, 4 = SPD, 5 = FDP, 6 = Green, 7 = Die Linke)

gen forecast_district = v266a

recode forecast_district (801=.) 
recode forecast_district (998=.) 

* Identify correct forecasts

gen correct_district_d = 0 if forecast_district!=district_outcome & forecast_district!=.
replace correct_district_d = 1 if forecast_district == district_outcome & forecast_district!=.


**********
** SAVE **
**********

save "de2009_gles.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion disagreement expertise coalscale interest care_whole age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1

* Regression analysis and percentage of correct and incorrect forecasts (district)

melogit correct_district_d discussion disagreement expertise pidscale_district interest age i.male education time enep i.reelected || wkrnr: , or
estimates table, star(.05 .01 .001)

tab correct_district_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Seat majority (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable expertise "Network expertise"
label variable pidscale_whole "National PID"
label variable pidscale_district "District PID"
label variable coalscale "Coalition preference"
label variable interest "Interest"
label variable care_whole "Caring about outcome"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable enep "ENEP"
label variable margin "Margin"
label variable reelected "Reelected"

quietly logistic correct_whole_d discussion disagreement expertise coalscale interest care_whole age i.male education time

estpost tabstat correct_whole_d discussion disagreement expertise coalscale interest care_whole age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "de2009_gles_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2009 German federal election (GLES), national level") replace

eststo clear

quietly melogit correct_district_d discussion disagreement expertise pidscale_district interest age i.male education time enep i.reelected || wkrnr: , or

estpost tabstat correct_district_d discussion disagreement expertise pidscale_district interest age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "de2009_gles_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2009 German federal election (GLES), district level") replace

eststo clear


*************************
** ADDITIONAL ANALYSES **
*************************

* Regression analysis (national)

logistic correct_whole_d i.network_main discussion disagreement expertise coalscale interest care_whole age i.male education time
estimates table, star(.05 .01 .001)

* Regression analysis (district)

melogit correct_district_d i.network_main discussion disagreement expertise pidscale_district interest age i.male education time enep i.reelected || wkrnr: , or
estimates table, star(.05 .01 .001)

log close